import {
  EventTypeEnum,
  MatchStatusEnum,
  RoundLoopTypeEnum,
  RoundNameTypeEnum,
  RoundOrganizationEnum,
  StagePlayerSourceTypeEnum,
  StageTypeEnum,
} from './common/enum'

// type EventType =
//   | '男单'
//   | '女单'
//   | '混单'
//   | '男双'
//   | '女双'
//   | '混双'
//   | '双打(不限性别)'
//   | '男团'
//   | '女团'
//   | '混团'
type EventType = EventTypeEnum
type TeamMatchType = 0 | 1 // '无' | '斯韦斯林杯赛制'
type PlayerType = 1 | 2
type StageType = StageTypeEnum // 淘汰赛, 循环赛
type RoundNameType = RoundNameTypeEnum // 'ABCD' | '1234' | '一二三四' // 命名方式
type RoundLoopType = RoundLoopTypeEnum // '单循环' | '双循环' // 循环次数
type RoundOrganizationType = RoundOrganizationEnum // '逆时针' | '顺时针' | '贝尔格' // 编排方式

// type EventTypeEnum = {
//   id: number
//   name: string
// }

export type User = {
  // 用户信息
  id: number
  name: string
  phone: string
  email: string
  idCard: string
  age: number
  gender: string
  address: string
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
  _key: number
}

export type Team = {
  id: number
  name: string
  userList: Array<User>
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
}

export type Tournament = Partial<{
  // 赛事
  // 比赛基本信息
  id: number
  name: string
  address: string
  fromDate: string
  endDate: string
  type: 1
  registrationFromDate: string
  registrationEndDate: string
  categories: Array<Category>
  status: 1
  concatUserList: Array<User> // 联系人
  adImage: string // 广告图
  detailImage: string // 详情图
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
}>

export type Category = Partial<{
  // 赛事分组
  // 比赛项目组(青年组，少年组，老年组)
  id: number
  name: string
  alias: string // 代号
  minAge: number
  maxAge: number
  registrationType: '实名' | '身份证' | '电话'
  events: Array<Event>
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
  _eventList: Array<string>
  _key: number
}>

export type Event = Partial<{
  // 比赛项目(男单/女单)
  id: number
  name: string
  type: EventType
  fee: number
  minPlayerCount: number
  maxPlayerCount: number
  stages: Array<EliminationStage & RoundStage> // 阶段
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
  eventToPlayers: EventToPlayer[]
}>

export type EventToPlayer = Partial<{
  id: number
  registrationIndex: string
  player: User
  player2: User
  team: Team
  player1Name: string
  player2Name: string
  teamName: string
  teamId: number
}>

export type Stage = Partial<{
  // 比赛阶段(小组赛，淘汰赛)
  id?: number
  type?: StageType // '循环赛1' | '淘汰赛2' // game-robin | knockout 赛制类型
  name?: string
  playerCount?: number
  playerType?: PlayerType // 参赛人员类型
  // groupCount: number
  // winnerCount: number
  teamMatchType?: TeamMatchType // 团队赛制赛制
  gameRuleId: number
  rallyRuleId: number
  groups?: Array<Group>
  createBy?: number
  createTime?: Date
  updateBy?: number
  updateTime?: Date
  fkEventId?: number
  eventType?: EventType
  players?: Array<EventToPlayer>
  stageOrderInEvent?: number
  stageEvents: StageEvent[]
  mustFinishMaxCountGame: boolean
  mustFinishMaxCountMatch: boolean
  mustCheckIn: boolean
  playerSourceType: StagePlayerSourceTypeEnum
  playerSourceStageId: number
}>

export type StageEvent = Partial<{
  id: number
  stageId: number
  eventOrder: number
  type: EventType
}>

export type EliminationStage = Partial<{
  // 淘汰赛
  eliminationWinnerCount?: number // 晋级数量
  eliminationSighType?: string // 签表顺序
}> &
  Stage

export type RoundStage = Partial<{
  // 循环赛
  roundNameType: RoundNameType // 命名方式
  roundGroupCount?: number
  roundGroupWinnerCount?: number
  roundLoopCount?: RoundLoopType // 循环次数
  roundOrganizationType?: RoundOrganizationType // 编排方式
}> &
  Stage

export type Group = {
  // 比赛阶段小组
  id: number
  name: string
  // playerList: Array<User>
  players: Array<User>
  groupPlayerScores: Array<GroupPlayerScore>
  matchInfos: Array<Match>
  teamList: Array<Team>
  teamMatchList: Array<TeamMatch>
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
}
export type GroupPlayerScore = {
  eliminationPosition: number
  groupId: number
  id: number
  loseGameCount: number
  loseMatchCount: number
  loseRallyCount: number
  player: EventToPlayer
  playerId: number
  registrationIndex: string
  winGameCount: number
  winMatchCount: number
  winRallyCount: number
}

export type Seeding = {
  // 抽签
  id: number
  player?: Array<User>
  team?: Team
  name?: string
}

export type Match = {
  // 对决
  id: number
  groupLoopIndex: number
  type: EventType
  matchTypeText: string
  // player1: Seeding | null
  // player2: Seeding | null
  // player3: Seeding | null
  // player4: Seeding | null
  isTeamMatch: boolean
  homePlayerId: number
  awayPlayerId: number
  homePlayer: EventToPlayer // 主队
  awayPlayer: EventToPlayer // 客队
  homePlayerName: string
  awayPlayerName: string
  homePlayerStatus: number
  awayPlayerStatus: number
  winner: EventToPlayer // 胜者
  winnerId: number
  homePlayerRally: number // 主队得分
  awayPlayerRally: number // 客队得分
  gameRuleId: number
  rallyRuleId: number
  games: Array<Game>
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
  estimatedStart: Date
  estimatedEnd: Date
  actualStart: Date
  actualEnd: Date
  playground: Playground
  subPlayground: Playground
  status: MatchStatusEnum
  groupName: string
  stageName: string
  subMatchInfos: Array<Match>
  groupMatchIndex: number
  rangeMin: number
  rangeMax: number
  mustFinishMaxCountGame: boolean
  mustFinishMaxCountMatch: boolean
}

export type Playground = {
  id: number
  name: string
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
  gymnasium: Gymnasium
}

export type Gymnasium = {
  id: number
  name: string
  address: string
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
  playgrounds: Array<Playground>
}

export type TeamMatch = {
  id: number
  teamMatchList: Array<Match>
} & Match

export type Game = {
  // 对决局
  id: number
  // matchId: number
  left: Seeding
  right: Seeding
  winner: Seeding
  homePlayerRally: number
  awayPlayerRally: number
  process: Array<Rally>
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
}

export type Rally = {
  // 得分局
  id: number
  player1: User
  player2: User
  player3: User
  player4: User
  leftRally: number
  rightRally: number
  createBy: number
  createTime: Date
  updateBy: number
  updateTime: Date
}

export type RallyRule = {
  id: number
  name: string
  gamePoint: number
  maxWinningRally: number
  minWinningRally: number
  restRallies: number[]
}

export type GameRule = {
  id: number
  name: string
  minWinGames: number
  maxWinGames: number
  description: string
}

export type PaginationType = {
  data: any[]
  total: number
  page: number
  limit: number
}

export type LoginResponse = {
  access_token: string
}
